package com.hogan.hoganproject.contacts;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.hogan.hoganproject.R;
import com.hogan.hoganproject.base.fragment.BaseFragment;
import com.hogan.hoganproject.contacts.adapter.ContactsAdapter;
import com.hogan.hoganproject.contacts.adapter.ContactsItemBean;
import com.hogan.hoganproject.contacts.mvp.ContactsControl;
import com.hogan.hoganproject.contacts.mvp.ContactsPresenter;
import com.hogan.hoganproject.pinyin.Binder;
import com.hogan.hoganproject.pinyin.IndexView;
import com.hogan.hoganproject.utils.PhoneUtils;

import java.util.List;


/**
 * Author: chenhu
 * Time: 2020/9/3 11:21
 * Description do somethings
 */
public class ContactsFragment extends BaseFragment<ContactsPresenter> implements ContactsControl.ContactsView {
    private ListView mListView;
    private IndexView mIndexView;
    private ContactsAdapter mContactsAdapter;

    public static ContactsFragment newInstance() {
        Bundle args = new Bundle();
        ContactsFragment fragment = new ContactsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutResID() {
        return R.layout.contacts_fragment;
    }

    @Override
    protected ContactsPresenter initPresenter() {
        mFragmentPresenter = new ContactsPresenter();
        mFragmentPresenter.onAttack(this);
        return mFragmentPresenter;
    }

    @Override
    protected void initView() {
        mListView = mBaseView.findViewById(R.id.contacts_list_view);
        mIndexView = mBaseView.findViewById(R.id.contacts_index_View);


        mFragmentPresenter.getContactsData();
    }

    @Override
    public void onUnsubscribe() {
        if (mFragmentPresenter != null) {
            mFragmentPresenter.onDetach();
        }
    }

    @Override
    public void onRecoverLoadData() {
        //要再次onAttack，不然view为null
  //      mFragmentPresenter.onAttack(this);
//        if (mFragmentPresenter != null) {
//            mFragmentPresenter.getContactsData();
//        }
    }

    @Override
    public void setContactsUI(final List<ContactsItemBean> itemBeanList) {
        mContactsAdapter = new ContactsAdapter(mContext, itemBeanList);
        mListView.setAdapter(mContactsAdapter);
        initBinder();
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PhoneUtils.callIntentPhone(mContext, itemBeanList.get(position).getUserPhone());
            }
        });
    }


    private void initBinder() {
        Binder binder = new Binder(mListView, mIndexView) {
            @Override
            public String getListItemKey(int position) {
                String name;
                try {
                    name = ((ContactsItemBean) (mListView.getAdapter().getItem(position))).getUserName();
                    if (name != null) {
                        return name;
                    } else {
                        return "error";
                    }
                } catch (Exception e) {
                    return e.toString();
                }
            }
        };
        binder.bind();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void showDialog() {
        mLoadingDialog.show();
    }

    @Override
    public void disDialog() {
        mLoadingDialog.dismiss();
    }

}
